package com.inter.insideinterservice.newservice;

import com.inter.entity.common.OrderPaymentManager;
import com.inter.entity.newpojo.Constant;
import com.inter.entity.newpojo.TurntoInterfaceUtils;
import com.inter.entity.newpojo.request.RequestBody.OrderMonthlyPackageRequestBody;
import com.inter.entity.newpojo.request.RequestHeader;
import com.inter.entity.newpojo.request.subRequest.OrderMonthlyPackageRequest;
import com.inter.entity.newpojo.response.subResponse.OrderMonthlyPackageResponse;
import com.inter.utils.HttpClientTool;
import com.inter.utils.JaxbUtil;
import org.apache.log4j.Logger;

/**
 * Demo 去北京下实际的单时的实现类
 *
 * @author lm
 * @date 2018/08/31
 */
public class TurnToOrderService {

    private static Logger logger = Logger.getLogger(TurnToOrderService.class);

    public OrderPaymentManager orderPackage(String deviceId, String globalOrder, String packageName, String packageCode, String packageType, String orderPeriod, String amount, String secret, String partnerCode) {

        try {
            // 请求参数的处理
            OrderMonthlyPackageRequestBody body = new OrderMonthlyPackageRequestBody();
            body.setDeviceId(deviceId);
            body.setGlobalOrder("0");
            body.setPackageName(packageName);
            body.setPackageCode(packageCode);
            body.setPackageType(packageType);
            body.setOrderPeriod(orderPeriod);
            body.setPayAmount(amount);
            //未支付
            body.setPayRst("1");
            //随意支付方式
            body.setPayType("");
            //packageType=0是日程套餐，不支持立即开通
            //下单时上发不开通的报文
            body.setIsOpen("0");
            body.setTestFlowFlag("0");
            //packageType！=0是流量套餐，应该要支持立即开通，上发报文就不要上发
            // 请求参数拼接
            OrderMonthlyPackageRequest request = new OrderMonthlyPackageRequest();
            TurntoInterfaceUtils utils = new TurntoInterfaceUtils();
            //拼装头信息
            RequestHeader header = utils.getRequestHeader(Constant.cmd_orderPackage, partnerCode);
            request.setHeader(header);
            request.setBody(body);
            String sign = utils.getSign(header, body, body.getClass(), secret);
            //logger.info("签名是：" + sign);
            request.setSign(sign);

            // 将参数转换成XML格式
            String sRequest = new JaxbUtil(OrderMonthlyPackageRequest.class).toXml(request, "utf-8");
            //logger.info("请求内容：" + sRequest);
            // 发送post请求，并接收返回结果
            String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
            //logger.info("返回结果：" + result);

            OrderMonthlyPackageResponse responseObj = new JaxbUtil(OrderMonthlyPackageResponse.class).fromXml(result, "utf-8");
            String retCode = responseObj.getHeader().getResultCode();
            String resultInfo = responseObj.getHeader().getResultInfo();

            if (true == Constant.RET_CODE_0000.equalsIgnoreCase(retCode)) {

                // 成功:北京回传的orderId
                String orderId = responseObj.getBody().getOrderId();
                String startData = responseObj.getBody().getOrderStartDate();
                String expireDate = responseObj.getBody().getOrderExpireDate();

                OrderPaymentManager user = new OrderPaymentManager();
                user.setOrderId(orderId);
                user.setOrderStartDate(startData);
                user.setOrderExpireDate(expireDate);
                user.setRetCode(retCode);
                user.setResultInfo(resultInfo);
                return user;

            } else {
                OrderPaymentManager user = new OrderPaymentManager();
                user.setRetCode(retCode);
                user.setResultInfo(resultInfo);
                return user;

            }
        } catch (Exception e) {
            e.printStackTrace();

        }
        return null;

    }


}
